<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_xunichong/model')->方法名();
*/
namespace jy_xunichong;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function sendgoods($order){
		if($hexiao==1 && $order['gift_type']){
			$order['total']=$order['total']?:1;
			$fee=$order['oldprice']*$order['total'];
			if($order['merchid']){
				model('jy_merch/member')->member_credit($order['merchid'],$order['mid'],'credit'.$order['gift_type'],$fee,true,'jy_xunigoods','虚拟充值');
			}else{
				model('core/member')->member_credit($order['mid'],'credit'.$order['gift_type'],$fee,true,'jy_xunigoods','虚拟充值');
			}
		}
		$update=array();
		if($hexiao==1){
			$update['status']=1;
			$update['hexiaotime']=TIMESTAMP;
		}elseif($hexiao==2){
			$update['status']=7;
		}
		$this->pdo_update('order',$update,array('id'=>$order['id']));
		return false;
	}
	public function sendcard($card,$code,$mid=0){
		$mid=$mid?$mid:$_SESSION['uid'];
		if($card['balance_type']==1){
			$money=(rand($card['balance_left']*100,$card['balance_right']*100))/100;
		}else{
			$money=$card['balance'];
		}
		if($card['type']){
			if($card['merchid']){
				model('jy_merch/member')->member_credit($card['merchid'],$mid,'credit'.$card['type'],$money,true,'jy_xunigoods','卡片兑换充值');
			}else{
				model('core/member')->member_credit($mid,'credit'.$card['type'],$money,true,'jy_xunigoods','卡片兑换充值');
			}
		}
		pdo_update('jy_xunichong_code',array('status'=>1),array('id'=>$code['id']));
		pdo_update('jy_xunichong_card',array('use +='=>1),array('id'=>$card['id']));
		pdo_insert('jy_xunichong_record',array('merchid'=>$card['merchid'],'key'=>$code['key'],'type'=>$card['type'],'balance'=>$money,'mid'=>$mid,'createtime'=>time(),'uniacid'=>$_SESSION['uniacid']));
		return false;
	}
	public function create($params){
		$this->order=$params;
		$this->order['createtime']=TIMESTAMP;
		$uid=$uid?$uid:$_SESSION['uid'];
		$order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'ordersn'=>$params['ordersn']?:get_ordersn($this->tablename('order'),'ordersn'),
			'mid'=>$_SESSION['uid'],
			'price'=>$params['price'],
			'oldprice'=>$params['oldprice'],
			'remark'=>trim($params['remark']),
			'goodsid'=>$params['goodsid'],
			'total'=>intval($params['total'])?:1,
			'gift_type'=>intval($params['gift_type']),
			'createtime'=>$params['createtime']?:TIMESTAMP,
		);
		$this->pdo_insert('order',$order);
		$order_id=$this->pdo_insertid();
		return $order_id;
	}
	//支付
	public function order_pay($order,$params,$url=''){
		$update=array(
				'status'=>$params['status']?:1,
				'paytype'=>$params['paytype'],
				'transid'=>$params['transid'],
		);
		if($params['paytype']=='hdfk'){
			$update['ispay']=0;
		}else{
			$update['ispay']=1;
			$update['paytime']=TIMESTAMP;
		}
		$this->pdo_update('order',$update,array('id'=>$order['id']));
		if($order['gift_type']){
			$this->sendgoods($order);
		}
		return true;
	}
	//订单取消
	public function order_cancel($order_id,$istask=false){
		$this->update('order',array('status'=>4),array('id'=>$order_id));
		return true;
	}
	//订单完成
	public function order_receive($order_id,$istask=false){
		$this->update('order',array('status'=>2),array('id'=>$order_id));
		return true;
	}
}
?>